package org.yuanqiframework.yuanqi.boot.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import org.yuanqiframework.yuanqi.boot.auth.AuthService;
import org.yuanqiframework.yuanqi.web.controller.BaseController;
import org.yuanqiframework.yuanqi.core.utils.StringUtils;
import org.yuanqiframework.yuanqi.web.entity.AjaxResult;
import org.yuanqiframework.yuanqi.boot.sys.dto.SysUrlWhitelistDTO;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysUrl;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysUrlWhitelist;
import org.yuanqiframework.yuanqi.boot.sys.service.SysUrlService;
import org.yuanqiframework.yuanqi.boot.sys.service.SysUrlWhitelistService;
import org.yuanqiframework.yuanqi.boot.sys.vo.SysUrlVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/sys/url")
public class SysUrlController extends BaseController {
    @Resource
    private SysUrlService sysUrlService;
    @Resource
    private SysUrlWhitelistService sysUrlWhitelistService;
    @Resource
    private AuthService authService;


    @GetMapping(value = "queryList")
    public AjaxResult<List<SysUrl>> queryList(@RequestParam(required = false) String keyword) {
        return AjaxResult.success(sysUrlService.list(new LambdaQueryWrapper<SysUrl>()
                .like(StringUtils.isNotEmpty(keyword), SysUrl::getHttpUrl, keyword)
                .or()
                .like(StringUtils.isNotEmpty(keyword), SysUrl::getClassName, keyword)
                .or()
                .like(StringUtils.isNotEmpty(keyword), SysUrl::getMethod, keyword)
                .orderByAsc(SysUrl::getHttpUrl)
        ));
    }


    @GetMapping(value = "queryWithRolesList")
    public AjaxResult<List<SysUrlVO>> queryWithRolesList(@RequestParam(required = false) String keyword) {
        return AjaxResult.success(sysUrlService.getAll(keyword));
    }


    @PostMapping(value = "addUrlToWhitelist")
    public AjaxResult<?> addUrlToWhitelist(@Valid @RequestBody List<SysUrlWhitelistDTO> dtos) {
        boolean add = sysUrlWhitelistService.add(dtos);
        if (add) {
            authService.flushUrlWhitelistCache();
        }
        return toAjax(add);
    }

    @PostMapping(value = "removeUrlFormWhitelist")
    public AjaxResult<?> removeUrlFormWhitelist(@Valid @RequestBody List<String> ids) {
        boolean remove = sysUrlWhitelistService.remove(ids);
        if (remove) {
            authService.flushUrlWhitelistCache();
        }
        return toAjax(remove);
    }


    @GetMapping(value = "queryWhitelist")
    public AjaxResult<List<SysUrlWhitelist>> queryWhitelist(@RequestParam(required = false) String keyword) {
        return AjaxResult.success(sysUrlWhitelistService.list(new LambdaQueryWrapper<SysUrlWhitelist>()
                .like(StringUtils.isNotEmpty(keyword), SysUrlWhitelist::getHttpUrl, keyword)
                .or()
                .like(StringUtils.isNotEmpty(keyword), SysUrlWhitelist::getHttpMethod, keyword)
                .or()
                .like(StringUtils.isNotEmpty(keyword), SysUrlWhitelist::getAllowAnonymous, keyword)
                .orderByAsc(SysUrlWhitelist::getHttpUrl)
        ));
    }
}